﻿using LoveEat.API.Read.Applications.Commands.Aquaculture;
using LoveEat.API.Read.DTOs;
using LoveEat.Domain.Aquaculture;
using LoveEat.Domain.Goods;
using LoveEat.ErrorCode;
using LoveEat.Infrastructure.Interfaces;
using MediatR;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace LoveEat.API.Read.Applications.CommandHandlers.Aquaculture
{
    public class AquacultureQueryCommandHandler : IRequestHandler<AquacultureQueryCommand, APIResult<List<AquacultureDTO>>>
    {
        private readonly IBaseRespository<GoodsModel> goodsRep;
        private readonly IBaseRespository<AquacultureModel> aquaRep;

        public AquacultureQueryCommandHandler(IBaseRespository<GoodsModel> goodsRep, 
            IBaseRespository<AquacultureModel> aquaRep)
        {
            this.goodsRep = goodsRep;
            this.aquaRep = aquaRep;
        }

        public Task<APIResult<List<AquacultureDTO>>> Handle(AquacultureQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<AquacultureDTO>> result=new APIResult<List<AquacultureDTO>>();
            result.Code=APIResultCode.查询成功;
            result.Message=APIResultCode.查询成功.ToString();

            var res = from a in goodsRep.GetAll()
                      join b in aquaRep.GetAll() on a.GoodsId equals b.AquacultureId
                      where !a.IsDelete  
                      select new AquacultureDTO
                      {
                          AquacultureId=b.AquacultureId,
                          variety=b.variety,
                          Source=b.Source,
                          Days=b.Days,
                          Brand=b.Brand,
                          Manager=b.Manager,
                          Releasedate=b.Releasedate,
                          Farm= b.Farm,
                          Charge= b.Charge,
                          Numberstock= b.Numberstock,
                          GoodsId=a.GoodsId,
                      };
            //排序
            res = res.OrderByDescending(x => x.AquacultureId);
            result.Data=res.ToList();



            return Task.FromResult(result);
        }
    }
}
